#!/usr/bin/perl
#
# castcopy - listener
# Copyright (C) 2007 Nick Bargnesi <nick at den-4.com>
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
use strict;
use IO::Socket::Multicast;
use Config::Simple;
use constant CONTROL_BUFFER_SIZE => 1024;
my $cfg_file = '/etc/castcopy.cfg';
my %config;
my $cfg = Config::Simple->import_from($cfg_file, %config) or die "Invalid configuration ($cfg_file): $!\n";
my $data_address = $cfg->param("net.data_address");
my $data_port = $cfg->param("net.data_port");
my $control_address = $cfg->param("net.control_address");
my $control_port = $cfg->param("net.control_port");
my $blocksize = $cfg->param("options.blocksize");
my $controldest = "$control_address:$control_port";

print "Listening, [CTRL-C] to quit.\n";
while (1) {
    my $data;
    my $sock = IO::Socket::Multicast->new(LocalPort=>$control_port, ReuseAddr=>1);
    $sock->mcast_add($control_address) or die "Could not set control group: $!\n";
    $sock->recv($data, CONTROL_BUFFER_SIZE) or die "Could not receive signal: $!\n";
    print ">> $data\n";
}

